﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Parylene_CVD.Service;
using Parylene_CVD.Commands;
using System.Windows;
using System.Security.Cryptography;
using Parylene_CVD.Tools;
using Parylene_CVD.DAL;
using Parylene_CVD.Model.DModels;
using Parylene_CVD.Model;

namespace Parylene_CVD.ViewModel
{
    public class LoginViewModel : ViewModelBase
    {
        public LoginViewModel(NavigationService HomeViewNavigationService) {
            _homeViewNavigationService = HomeViewNavigationService;
            LoginCommand = new RelayCommand(Login);
            TitlesSet();
        }
        private readonly NavigationService _homeViewNavigationService;
        // 用户名
        private string _username;
        public string Username
        {
            get => _username;
            set
            {
                _username = value;
                OnPropertyChanged(nameof(Username));
            }
        }

        // 密码
        private string _password;
        public string Password
        {
            get => _password;
            set
            {
                _password = value;
                OnPropertyChanged(nameof(Password));
            }
        }

        private UserDAL userDAL = new UserDAL();

        // 点击登录按钮命令
        public ICommand LoginCommand { get; }
        private void Login(object parameter)
        {
            // _homeViewNavigationService.Navigate();
            User user = userDAL.getByUserName(Username);
            if (user != null && PasswordEncryption.Decrypt(user.Password) == Password)
            {
                GlobalVariables.LoginUserRole = user.Role;
                _homeViewNavigationService.Navigate();
            }
            else
            {
                Username = String.Empty;
                Password = String.Empty;
                MessageBox.Show($"账号或密码错误，请重新输入！");
            }

        }


        //页面显示文本定义与初始化:
        public string? Title { get; set; }
        public string? UsernameTitle { get; set; }
        public string? PasswordTitle { get; set; }
        public string? LoginTitle { get; set; }
        

        private void TitlesSet()
        {
            Title = "苏州凯瑞纳米科技有限公司";
            UsernameTitle = "账号:";
            PasswordTitle = "密码:";
            LoginTitle = "登录";
        }


        
    }
}
